﻿using System;
using System.Collections.Generic;
using io = System.IO;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using System.Reflection;
using System.IO;

namespace Sixeyed.Guidance.CodeGeneration.Helpers
{
    /// <summary>
    /// Helper class for retrieving path information
    /// </summary>
    public static class PathHelper
    {
        /// <summary>
        /// Returns the Templates directory path for the given Guidance package
        /// </summary>
        /// <param name="package">Guidance package to examine</param>
        /// <returns>Template path</returns>
        public static string GetTemplateDirectory(GuidancePackage package)
        {
            string basePath = null;
            if (package != null && !string.IsNullOrEmpty(package.BasePath))
            {
                basePath = package.BasePath;
            }
            else
            {
                basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", null));
            }
            return GetTemplateDirectory(basePath);
        }

        private static string GetTemplateDirectory(string baseDirectory)
        {
            string templateDirectory = baseDirectory;
            if (io.Directory.Exists(baseDirectory))
            {
                templateDirectory = string.Format(@"{0}\Templates\Text", baseDirectory);
                if (io.Directory.Exists(templateDirectory))
                {
                    baseDirectory = templateDirectory;
                }
            }
            return templateDirectory;
        }

        private static string GetTemplateDirectory()
        {
            string basePath = Assembly.GetExecutingAssembly().CodeBase;
            return GetTemplateDirectory(basePath);
        }
    }
}
